import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser';
import * as session from 'express-session';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { engine } from 'express-handlebars';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { MyLogger } from './my-logger';
import { ExtendedConsoleLogger } from './extended-console-logger';
import { I18nValidationPipe, I18nValidationExceptionFilter } from 'nestjs-i18n';
import * as helpers from 'src/shared/helpers';
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    // logger: new ExtendedConsoleLogger()
    // bufferLogs: true,
  });
  //console.log(app.get('LOGGER_CONFIG'))
  // app.useLogger(app.get(MyLogger))
  app.useStaticAssets(join(__dirname, '..', 'public'));
  // app.useStaticAssets(join(__dirname, '..', 'uploads'));

  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.engine(
    'hbs',
    engine({
      extname: '.hbs',
      helpers,
    }),
  );
  app.set('view engine', 'hbs');
  app.use(cookieParser());
  app.use(
    session({
      secret: 'secret-key',
      resave: true,
      saveUninitialized: true,
      cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7,
      },
    }),
  );
  // app.useGlobalPipes(new ValidationPipe({transform: true}))
  app.useGlobalPipes(new I18nValidationPipe({ transform: true })); // 多语言管道
  app.useGlobalFilters(
    new I18nValidationExceptionFilter({ detailedErrors: true }),
  ); // 多语言过滤器
  const config = new DocumentBuilder()
    .setTitle('CMS API')
    .setDescription('COM API 描述')
    .setVersion('1.0')
    .addTag('CMS')
    .addCookieAuth('connect.sid') // 添加cookier认证方式
    .addBearerAuth({
      // 添加Bearer认证方式 在请求头里添加 Authorization: Bearer token
      type: 'http',
      scheme: 'bearer',
    })
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api-doc', app, document);
  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
